package com.zcxy360.sandtable.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcxy360.sandtable.domain.CourseExample;
import com.zcxy360.sandtable.domain.CourseExample.Criteria;
import com.zcxy360.sandtable.domain.CourseMapper;
import com.zcxy360.sandtable.domain.CourseStudent;
import com.zcxy360.sandtable.domain.CourseStudentMapper;
import com.zcxy360.sandtable.util.ShiroUtil;
import com.zcxy360.sandtable.util.StringUtil;

@Transactional
@Service
public class CourseStudentService {

	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private CourseMapper courseMapper;
	@Autowired
	private CourseStudentMapper courseStudentMapper;

	public Object getByCourseId(Integer courseId) {
		return courseStudentMapper.selectByPrimaryKeyWithCascade(courseId,ShiroUtil.getUser().getUserId());
	}
	
	public Object getForCompletion(Integer courseId, Integer userId) {
		return courseStudentMapper.selectForCompletion(courseId,userId);
	}
	
	public Object list(CourseStudent courseStudent, String key, String orderByClause,Integer pageNum, Integer pageSize,
			Integer navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		List<CourseStudent> courseStudents = courseStudentMapper.listWithCascade(courseStudent,StringUtil.wrapper(key),orderByClause);
		return new PageInfo<>(courseStudents, navigatePages);
	}

	public CourseStudent post(CourseStudent courseStudent) {
		if(courseStudent.getUserId() == null) {
			courseStudent.setUserId(ShiroUtil.getUser().getUserId());
		}
		courseStudentMapper.insertSelective(courseStudent);
		return courseStudent;
	}

	public boolean update(CourseStudent courseStudent) { 
		courseStudentMapper.updateByPrimaryKeySelective(courseStudent);
		return true;
	}

	public boolean delete(String[] keys) {
		List<CourseStudent> courseStudents = new ArrayList<>();
		CourseStudent courseStudent;
		String[] split;
		Integer courseId;
		Integer userId;
		Set<Integer> courseIds = new HashSet<>();
		for(String key:keys) {
			courseStudent = new CourseStudent();
			split = key.split("_");
			courseId = Integer.valueOf(split[0]);
			userId = Integer.valueOf(split[1]);
			courseIds.add(courseId);
			courseStudent.setCourseId(courseId);
			courseStudent.setUserId(userId);
			courseStudents.add(courseStudent);
		}
		CourseExample ex = new CourseExample();
		Criteria or = ex.or();
		or.andCourseIdIn(new ArrayList<>(courseIds));
		or.andTypeNotEqualTo(0);
		if(courseMapper.countByExample(ex) > 0) {
			throw new RuntimeException("已开课或已结束课程不允许删除成员");
		}
		courseStudentMapper.deleteBatch(courseStudents);
		return true;
	}

	public boolean join(Integer courseId) {
		Integer userId = ShiroUtil.getUser().getUserId();
		if(courseStudentMapper.countJoinedCourseByUserId(userId) > 0) {
			throw new RuntimeException("有课程未完成,不允许加入新课程");
		}
		CourseStudent courseStudent = new CourseStudent();
		courseStudent.setCourseId(courseId);
		courseStudent.setUserId(userId);
		courseStudent.setJoinTime(new Date());
		courseStudentMapper.insertSelective(courseStudent);
		return true;
	}

	public boolean mark(String courseStudentJson) {
		JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, CourseStudent.class);
		List<CourseStudent> courseStudents = null;
		try {
			courseStudents = objectMapper.readValue(courseStudentJson, javaType);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("解析数据失败");
		}
		Date now = new Date();
		for(CourseStudent courseStudent : courseStudents) {
			courseStudent.setIsPublished(true);
			courseStudent.setPublishTime(now);
		}
		courseStudentMapper.updateBatch(courseStudents);
		return true;
	}

}
